﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.GUI.Users.Common;
using Xmobile.Users;
using Portal.User.Security;

namespace Portal.GUI.Administrator.GenerateTabs
{
	
   

	//public delegate void EditCategoryEventHanlder(EditCategoryEventArgs e);
	//public delegate void CancelEditCategoryEventHandler();

	/// <summary>
	///		Summary description for CategoryEdit.
	/// </summary>
	public partial class CategoryEdit : System.Web.UI.UserControl
	{
		
		protected void Page_Load(object sender, System.EventArgs e)
		{
            if (!IsPostBack)
            {
                Portal.BO.Editoral.Category.CategoryHelper.Treebuild(cboCat);
                Portal.BO.Editoral.EditionType.EditionTypeHelper.SelectAllForDropdownlist(drEditionType);
                //odsEditionType.Select();
                //drEditionType.DataBind();

                if (Request.QueryString["parent"] != null)
                    cboCat.SelectedValue = Request.QueryString["parent"].ToString();
                if (Request.QueryString["NewsRef"] != "")
                {
                    btnSave.Visible = false;
                    int intCurrentCat = 0;
                    try
                    {
                        intCurrentCat = Convert.ToInt32(Request.QueryString["NewsRef"].ToString());
                    }
                    catch { }
                    hdCatID.Value = intCurrentCat.ToString();
                    BindCatForm(Portal.BO.Editoral.Category.CategoryHelper.getCatInfo(intCurrentCat));
                }
                else
                    btnUpdate.Visible = false;

                BindChannel();
            }
		}

        private void BindChannel()
        {
            DataTable dt = new DataTable();
            /*
            //nếu là admin kênh thì load kênh của admin đó có
            if (userID == Const.Global_Admin || userID == Const.TONG_BIEN_TAP)
            {
                using (MainDB db = new MainDB())
                {
                    ddlChannel.DataSource = db.ChannelCollection.GetAllAsDataTable();
                }
            }
            else // nếu không phải admin kênh , load toàn bộ kênh
            {
                MainSecurity ms = new MainSecurity();
                ddlChannel.DataSource = ms.GetChannelAsTable(userID);
            }
            */
            using (MainDB db = new MainDB())
            {
                dt = db.ChannelCollection.GetAllAsDataTable();
            }
            ddlChannel.DataSource = dt;
            ddlChannel.DataBind();
        }
        private void BindCatForm(ArrayList _arr)
        {
            try
            {
                txtCategoryName.Text = _arr[0].ToString();
                if(_arr[1] != null)
                txtCategoryDescription.Text = _arr[1].ToString();

                if(_arr[2] != null)
                txtCategoryDisplayURL.Text = _arr[2].ToString();

                if(_arr[3] != null)
                cboCat.SelectedValue = _arr[3].ToString();

                if(_arr[4] != null)
                chkIsColumn.Checked = Convert.ToBoolean(_arr[4]);

                if(_arr[5] != null)
                chkIsHidden.Checked = Convert.ToBoolean(_arr[5]);

                //SonPC

            if (_arr[8] != null)
                drEditionType.SelectedValue = _arr[8].ToString();
                //SonPC
            }
            catch { }
            
            
        }
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            objCatSource.Insert();
            Response.Redirect("~/category/catlist.chn");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            bool checkUpdate = Portal.BO.Editoral.Category.CategoryHelper.CheckUpdateCate(Convert.ToInt32(hdCatID.Value), Convert.ToInt32(cboCat.SelectedValue), true);
            if (checkUpdate)
            {
                objCatSource.Update();
                Response.Redirect("~/category/catlist.chn");
            }
            else
                Page.RegisterClientScriptBlock("category", "<script language='javascript'>alert('Chuyên mục cha không hợp lệ')</script>");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/category/catlist.chn");
        }
	}
}
